/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.jsimplemarket.wicket.pages;

import com.googlecode.jsimplemarket.model.AbstractNomenclator;
import com.googlecode.jsimplemarket.service.GenericService;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Alexandru.Codescu
 */
public class NomenclatorEditPage<T extends AbstractNomenclator> extends DefaultEditPage {
    @SpringBean(name = "genericService")
    private GenericService<T> service;
    
    private T entity;
    
    public NomenclatorEditPage(Class<T> clazz) {
        try {
            entity = clazz.newInstance();
        } catch (IllegalAccessException ex) {
            throw new ExceptionInInitializerError(ex);
        } catch (InstantiationException ex) {
            throw new ExceptionInInitializerError(ex);
        }
        init(clazz);
    }
    
    public NomenclatorEditPage(Class<T> clazz, Long id) {
        entity = service.getById(clazz, id);
        init(clazz);
    }
    
    private void init(final Class<T> clazz) {
        Form<T> form = new Form<T>("form");
        form.setModel(new CompoundPropertyModel<T>(entity));
        
        TextField<String> tfCod = new TextField<String>("cod");
        tfCod.setRequired(true);
        form.add(tfCod);
        
        TextField<String> tfDenumire = new TextField<String>("denumire");
        tfDenumire.setRequired(true);
        form.add(tfDenumire);
        
        final DateTextField dtfDataInceput = new DateTextField("dataInceput");
        dtfDataInceput.add(new DatePicker());
        form.add(dtfDataInceput);
        
        final DateTextField dtfDataSfarsit = new DateTextField("dataSfarsit");
        dtfDataSfarsit.add(new DatePicker());
        form.add(dtfDataSfarsit);
        
        form.add(new AbstractFormValidator() {

            @Override
            public FormComponent<?>[] getDependentFormComponents() {
                return new FormComponent[] {dtfDataInceput, dtfDataSfarsit};
            }

            @Override
            public void validate(Form<?> form) {
                // dataInceput < dataSfarsit
                if ((entity.getDataInceput() != null) && (entity.getDataSfarsit() != null)) {
                    if (entity.getDataInceput().after(entity.getDataSfarsit())) {
                        dtfDataInceput.error(NomenclatorEditPage.this.getString("error.date"));
                    }
                }
            }
        });
        
        form.add(new SubmitLink("submit") {

            @Override
            public void onSubmit() {
                if (entity.getId() == null) {
                    service.insert(entity);
                } else {
                    service.update(entity);
                }
                setResponsePage(new NomenclatorViewPage(clazz));
            }
            
        });
        
        form.add(new Link("renuntare") {

            @Override
            public void onClick() {
                setResponsePage(new NomenclatorViewPage(clazz));
            }
            
        });
        
        add(form);
    }
}
